#include "hat.h"
#include "terrain.h"

class CSnowman;
class CPlayer;

void Hat::OnPrepare() 
{	
	// perform collision detection from this entity with all other objects in world
	ProcessCollisions(FindRoot());
}

void Hat::Load()
{
	// load model
	CMD2Model::Load("..\\..\\res\\models\\hat\\hat.md2", "..\\..\\res\\models\\hat\\hat.pcx");
	isCollected = false;
}

void Hat::OnCollision(CObject *collisionObject)
{
		// as long as the model is not dying, it can collide with other objects
	if (modelState != MODEL_DIE)
	{
		// if the hat collides with an enemy
		if ((typeid(*collisionObject) == typeid(CSnowman)))
		{
			//aiState = AI_UNCARING;
		}

		// if this enemy collides with the terrain (always)
		else	if (typeid(*collisionObject) == typeid(CTerrain))
		{
			position.y = ((CTerrain*)collisionObject)->GetHeight(position.x, position.z);
			if (position.x <= ((CTerrain*)collisionObject)->GetScanDepth())
				position.x = ((CTerrain*)collisionObject)->GetScanDepth();
			if (position.x >= ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth())
				position.x = ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth();
			if (position.z <= ((CTerrain*)collisionObject)->GetScanDepth())
				position.z = ((CTerrain*)collisionObject)->GetScanDepth();
			if (position.z >= ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth())
				position.z = ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth();
		}
		// if the hat is collected by the player and hasn't already been collected
		else if (typeid(*collisionObject) == typeid(CPlayer) && !isCollected)
		{
			incrementHatsCollected();
			
			isCollected = true;
			isDead = true;
		}
	}
}

int Hat::hatsCollected = 0;

// Ian Bell - 12/5/2012
void Hat::setHatsCollected(int hats)
{
	hatsCollected = hats;
}

// Ian Bell - 12/5/2012
int Hat::getHatsCollected()
{
	return hatsCollected;
}

// Ian Bell - 12/5/2012
void Hat::incrementHatsCollected()
{
	hatsCollected++;
}

void Hat::OnDraw(CCamera *camera)
{
	// if the rocket has not yet exploded, draw the rocket model
		glEnable(GL_TEXTURE_2D);
		glColor3f(1.0, 1.0, 1.0);
		glTranslatef(position.x, position.y+1, position.z);
		glScalef(3.0f, 3.0f, 3.0f);
		glRotatef(-90, 1.0, 0.0, 0.0);
		RenderFrame(0);
		glDisable(GL_TEXTURE_2D);
}